Fix inferred name of `src/main.rs`
authorAlex Crichton <alex@alexcrichton.com>
Thu, 17 Jul 2014 01:44:30 +0000 (18:44 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 17 Jul 2014 01:44:30 +0000 (18:44 -0700)
The crate name should be the package name, not `main`.

Closes #207

src/cargo/ops/cargo_run.rs
src/cargo/sources/git/utils.rs
src/cargo/util/toml.rs
tests/support/mod.rs
tests/test_cargo_compile.rs
tests/test_cargo_compile_git_deps.rs
tests/test_cargo_cross_compile.rs
tests/test_cargo_run.rs
tests/test_cargo_test.rs
tests/test_shell.rs

index 6b6198a0d1d97475e96e8522e9f2e36f37c41363..dcf2e1ab707173721a638c75c82c735612d68994 100644 (file)
@@ -2,6 +2,8 @@ use std::os;
 
 use ops;
 use util::{CargoResult, human, process, ProcessError};
+use core::source::Source;
+use sources::PathSource;
 
 pub fn run(manifest_path: &Path,
            options: &mut ops::CompileOptions,
@@ -10,8 +12,12 @@ pub fn run(manifest_path: &Path,
         return Err(human("`src/main.rs` must be present for `cargo run`"))
     }
 
+    let mut src = PathSource::for_path(&manifest_path.dir_path());
+    try!(src.update());
+    let root = try!(src.get_root_package());
+
     try!(ops::compile(manifest_path, options));
-    let exe = manifest_path.dir_path().join("target/main");
+    let exe = manifest_path.dir_path().join("target").join(root.get_name());
     let exe = match exe.path_relative_from(&os::getcwd()) {
         Some(path) => path,
         None => exe,
index e90c49e4ac295a385aa35bd6b4d2e0da315e5c68..b8c0ef15a3af93b5c0e9c759d6133ce83fab0f2d 100644 (file)
@@ -1,6 +1,5 @@
 use std::fmt;
 use std::fmt::{Show,Formatter};
-use std::str;
 use std::io::{UserDir};
 use std::io::fs::{mkdir_recursive,rmdir_recursive};
 use serialize::{Encodable,Encoder};
@@ -311,6 +310,6 @@ fn git_output(path: &Path, str: String) -> CargoResult<String> {
 }
 
 fn to_str(vec: &[u8]) -> String {
-    str::from_utf8_lossy(vec).to_string()
+    String::from_utf8_lossy(vec).into_string()
 }
 
index a2fd7b369c3f154284ade2c65c127a45f6edfeb6..96b43f49304642bf98ba7f173c56ec697993712c 100644 (file)
@@ -17,6 +17,7 @@ use util::{CargoResult, Require, human};
 
 #[deriving(Clone)]
 pub struct Layout {
+    root: Path,
     lib: Option<Path>,
     bins: Vec<Path>,
     examples: Vec<Path>,
@@ -70,6 +71,7 @@ pub fn project_layout(root_path: &Path) -> Layout {
     try_add_files(&mut tests, root_path, "tests");
 
     Layout {
+        root: root_path.clone(),
         lib: lib,
         bins: bins,
         examples: examples,
@@ -237,7 +239,8 @@ fn inferred_lib_target(name: &str, layout: &Layout) -> Option<Vec<TomlTarget>> {
 
 fn inferred_bin_targets(name: &str, layout: &Layout) -> Option<Vec<TomlTarget>> {
     Some(layout.bins.iter().filter_map(|bin| {
-        let name = if bin.as_str() == Some("src/main.rs") {
+        let name = if bin.as_vec() == b"src/main.rs" ||
+                      *bin == layout.root.join("src/main.rs") {
             Some(name.to_string())
         } else {
             bin.filestem_str().map(|f| f.to_string())
index 30246736224ae2bbf6a2781a5ba3ba97b7c69b68..1b025d8157abb13d25c39656d44cf970f1799fac 100644 (file)
@@ -1,5 +1,3 @@
-// use std::io::fs::{mkdir_recursive,rmdir_recursive};
-use std;
 use std::io;
 use std::io::fs;
 use std::io::process::{ProcessOutput};
@@ -305,7 +303,7 @@ impl Execs {
                                             `{}`\n\n\
                                             other output:\n\
                                             `{}`", description, actual, out,
-                                            str::from_utf8_lossy(extra)))
+                                            String::from_utf8_lossy(extra)))
                     }
                 }
             }
@@ -358,7 +356,7 @@ impl<'a> ham::Matcher<&'a [u8]> for ShellWrites {
         -> ham::MatchResult
     {
         println!("{}", actual);
-        let actual = std::str::from_utf8_lossy(actual);
+        let actual = String::from_utf8_lossy(actual);
         let actual = actual.to_string();
         ham::expect(actual == self.expected, actual)
     }
index db114ac840ababc951848eaa1530527a3aaca87c..2a3290b2eda2d7f23f49e91b326d9d68960d7796 100644 (file)
@@ -1209,3 +1209,19 @@ test!(release_build_ndebug {
                 execs().with_status(0));
     assert_that(process(p.bin("release/foo")), execs().with_stdout("fast\n"));
 })
+
+test!(inferred_main_bin {
+    let p = project("world")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/main.rs", r#"
+            fn main() {}
+        "#);
+
+    assert_that(p.cargo_process("cargo-build"), execs().with_status(0));
+    assert_that(process(p.bin("foo")), execs().with_status(0));
+})
index b0ae8c26756286de54b4cc51d3d58ce1a85688d1..3a0f52b2c7c9ee8b06bdb6e5df31354acf8491f8 100644 (file)
@@ -475,8 +475,8 @@ test!(two_revs_same_deps {
     // TODO: -j1 is a hack
     assert_that(foo.cargo_process("cargo-build").arg("-j").arg("1"),
                 execs().with_status(0));
-    assert_that(&foo.bin("main"), existing_file());
-    assert_that(foo.process(foo.bin("main")), execs().with_status(0));
+    assert_that(&foo.bin("foo"), existing_file());
+    assert_that(foo.process(foo.bin("foo")), execs().with_status(0));
 })
 
 test!(recompilation {
index e049cfd1445f81fd877a09f78bfb4e6a1166ffc9..e7978e2a13ae8950a849dfef1486d7c4ebd8a847 100644 (file)
@@ -70,10 +70,10 @@ test!(simple_deps {
     let target = alternate();
     assert_that(p.cargo_process("cargo-build").arg("--target").arg(target),
                 execs().with_status(0));
-    assert_that(&p.target_bin(target, "main"), existing_file());
+    assert_that(&p.target_bin(target, "foo"), existing_file());
 
     assert_that(
-      process(p.target_bin(target, "main")),
+      process(p.target_bin(target, "foo")),
       execs().with_status(0));
 })
 
@@ -146,10 +146,10 @@ test!(plugin_deps {
     let target = alternate();
     assert_that(foo.cargo_process("cargo-build").arg("--target").arg(target),
                 execs().with_status(0));
-    assert_that(&foo.target_bin(target, "main"), existing_file());
+    assert_that(&foo.target_bin(target, "foo"), existing_file());
 
     assert_that(
-      process(foo.target_bin(target, "main")),
+      process(foo.target_bin(target, "foo")),
       execs().with_status(0));
 })
 
@@ -226,10 +226,10 @@ test!(plugin_to_the_max {
     let target = alternate();
     assert_that(foo.cargo_process("cargo-build").arg("--target").arg(target),
                 execs().with_status(0));
-    assert_that(&foo.target_bin(target, "main"), existing_file());
+    assert_that(&foo.target_bin(target, "foo"), existing_file());
 
     assert_that(
-      process(foo.target_bin(target, "main")),
+      process(foo.target_bin(target, "foo")),
       execs().with_status(0));
 })
 
index 355884e441668dcced76a23bdaeb1844370ea2d3..b4ec89c51214d03c774741ee98fd4e9dbca19b12 100644 (file)
@@ -22,14 +22,14 @@ test!(simple {
     assert_that(p.cargo_process("cargo-run"),
                 execs().with_status(0).with_stdout(format!("\
 {compiling} foo v0.0.1 (file:{dir})
-{running} `target{sep}main`
+{running} `target{sep}foo`
 hello
 ",
         compiling = COMPILING,
         running = RUNNING,
         dir = p.root().display(),
         sep = path::SEP).as_slice()));
-    assert_that(&p.bin("main"), existing_file());
+    assert_that(&p.bin("foo"), existing_file());
 })
 
 test!(simple_with_args {
index 7985d712fcf397f0792c4df0cf098712a8dca419..17c551345637e6fc37f034c2356d126d28ff4a4d 100644 (file)
@@ -49,6 +49,10 @@ test!(test_with_lib_dep {
             name = "foo"
             version = "0.0.1"
             authors = []
+
+            [[bin]]
+            name = "baz"
+            path = "src/main.rs"
         "#)
         .file("src/lib.rs", "
             pub fn foo(){}
@@ -81,6 +85,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured";
     let head = format!("{compiling} foo v0.0.1 (file:{dir})",
                        compiling = COMPILING, dir = p.root().display());
 
+    println!("{}", out);
     assert!(out == format!("{}\n\n{}\n\n\n{}\n\n", head, bin, lib).as_slice() ||
             out == format!("{}\n\n{}\n\n\n{}\n\n", head, lib, bin).as_slice());
 })
index e7dc72eb7b59e6686bfa4490fb2d32022bc2416b..d1211ab2bacbe54109c6ea1b6ffda92cce0fe2dd 100644 (file)
@@ -1,7 +1,6 @@
 use support::{ResultTest,Tap,shell_writes};
 use hamcrest::{assert_that};
 use std::io::{MemWriter, BufWriter, IoResult};
-use std::str::from_utf8_lossy;
 use cargo::core::shell::{Shell,ShellConfig};
 use term::{Terminal,TerminfoTerminal,color};
 
@@ -56,5 +55,5 @@ fn colored_output<S: Str>(string: S, color: color::Color) -> IoResult<String> {
     try!(term.write_str(string.as_slice()));
     try!(term.reset());
     try!(term.flush());
-    Ok(from_utf8_lossy(term.get_ref().get_ref()).to_string())
+    Ok(String::from_utf8_lossy(term.get_ref().get_ref()).to_string())
 }